package com.oceansoft.core.jxkh.controller;

import com.oceansoft.core.common.constant.Constant;
import com.oceansoft.core.common.util.Pagination;
import com.oceansoft.core.domain.Message;
import com.oceansoft.core.jxkh.domain.Khmc;
import com.oceansoft.core.jxkh.service.IKhlxService;
import com.oceansoft.core.jxkh.service.IKhmcService;
import com.oceansoft.core.jxkh.service.IKhyService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * Created by OS on 2017/12/14.
 */
@Controller
@RequestMapping("/khmc")
public class KhmcController {

    @Resource
    private IKhmcService khmcService;

    @Resource
    private IKhlxService khlxService;

    @Resource
    private IKhyService khyService;

    @RequestMapping(value="allKhmcInfo.json")
    public void allKhmcInfo(HttpServletRequest request, ModelMap modelMap){
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        String order = request.getParameter("order");
        HashMap<String, String> paramap = new HashMap<String, String>();
        paramap.put("offset",offset);
        paramap.put("limit",limit);
        paramap.put("order",order);

        Pagination khmcPag= khmcService.findAllkhmc(paramap);
        modelMap.addAttribute("rows",khmcPag.getList());
        modelMap.addAttribute("total",khmcPag.getTotalRows());
    }

    @RequestMapping(value="editKhmcInfo",method = RequestMethod.POST)
    public void editKhmcInfo(HttpServletRequest request, ModelMap modelMap,Khmc khmc){
        Message message = new Message("操作成功");
        message.setStatus(Constant.RESULT_SUCC);
        try {
            khmcService.editKhmcInfo(khmc);
        }catch (Exception e){
            e.printStackTrace();
            message.setStatus(Constant.RESULT_FAILD);
            message.setBody(e.getMessage());
        }
        modelMap.addAttribute("data",message);
    }

    @RequestMapping(value="findKhmcInfo.html")
    public String findKhmcInfo(int dbid,ModelMap modelMap){
        Khmc khmc = khmcService.findKhmcInfoByDbid(dbid);
        List list = khlxService.findAllKhlxName();
        modelMap.addAttribute("khlxNameList",list);
        modelMap.addAttribute("khmc",khmc);
        return "jump/khmcsz_bj";
    }

    @RequestMapping(value="deleteKhmcInfo.json")
    public void deleteKhmcInfo(int dbid, ModelMap modelMap){
        Message message = new Message("操作成功");
        message.setStatus(Constant.RESULT_SUCC);
        try {
            khmcService.deleteKhmcInfoByDbid(dbid);
        }catch (Exception e){
            e.printStackTrace();
            message.setStatus(Constant.RESULT_FAILD);
            message.setBody(e.getMessage());
        }
        modelMap.addAttribute("data",message);
    }

    @RequestMapping(value="addKhmcInfo.json",method =RequestMethod.POST)
    public void addKhmcInfo(Khmc khmc, ModelMap modelMap){
        Message message = new Message("操作成功");
        message.setStatus(Constant.RESULT_SUCC);
        try {
            khmcService.addKhmcInfo(khmc);
        }catch (Exception e){
            e.printStackTrace();
            message.setStatus(Constant.RESULT_FAILD);
            message.setBody(e.getMessage());
        }
        modelMap.addAttribute("data",message);
    }

    @RequestMapping(value="batchDeleteKhmcInfo.json")
    public void batchDeleteKhmcInfo(int[] dbids,ModelMap modelMap){
        Message message = new Message("操作成功");
        message.setStatus(Constant.RESULT_SUCC);
        try {
            khmcService.batchDeleteKhmcInfo(dbids);
        }catch (Exception e){
            e.printStackTrace();
            message.setStatus(Constant.RESULT_FAILD);
            message.setBody(e.getMessage());
        }
        modelMap.addAttribute("data",message);
    }

    /*
    * 考核名称设置
   * */
    @RequestMapping(value="khmcsz.html")
    public String khmcsz(HttpServletRequest request, ModelMap modelMap) {
        List khyNameList = khyService.findAllKhyName();
        List list = khlxService.findAllKhlxName();
        modelMap.addAttribute("khlxNameList",list);
        return "jump/khmcsz";
    }
}
